package at.technikum_wien.embsys.aat.PriscillaCore.component;

import java.util.Iterator;
import java.util.List;

import at.technikum_wien.embsys.aat.PriscillaCore.enums.EnoceanComponentType;
import at.technikum_wien.embsys.aat.PriscillaCore.event.FrameEventGetGatewayID;
import at.technikum_wien.embsys.aat.PriscillaCore.link.event.LinkListener;
import at.technikum_wien.embsys.aat.PriscillaCore.telegram.Telegram;



/**
 * Message for receiving the gateway device ID
 * 
 * This "component" will be created, if the
 * gateway returns its device ID
 * 
 * @author B. Aigner
 *
 */
public class GateWayID implements EnoceanComponent {

	Telegram telegram;
	List<?> eventListener;
	String gatewayID;

	/**
	 * Default constructor 
	 * 
	 * Only this constructor is available, a telegram creation like other components
	 * is not necessary.
	 * 
	 * @param telegram Received telegram
	 * @param listener Event subscriber list
	 */
	public GateWayID(Telegram telegram, List<?> listener) {
		super();
		this.telegram = telegram;
		this.eventListener = listener;
		
		this.gatewayID = telegram.getxData3() + telegram.getxData2() + telegram.getxData1() + telegram.getxData0();
		fireFrame();
	}

	/**
	 * Fire the gateway ID event
	 */
	void fireFrame() {
		Iterator<?> listen = eventListener.iterator();
		final FrameEventGetGatewayID frameEventGetGatewayID = new FrameEventGetGatewayID(this, gatewayID);
		while (listen.hasNext()) {
			((LinkListener) listen.next()).frameReceived_GatewayID(frameEventGetGatewayID);
		}
	}

	boolean checkBit(int value, int bitNr) {
		return (value & (1 << bitNr)) != 0;
	}

	int setBit(int value, int bitNr) {
		return value | (1 << bitNr);
	}

	int clrBit(int value, int bitNr) {
		return value & (~(1 << bitNr));
	}

	@Override
	public String getDeviceID() {
		return null;
	}

	@Override
	public String getORG() {
		return null;
	}

	@Override
	public EnoceanComponentType getType() {
		return null;
	}

	@Override
	public void setDeviceID(String deviceID) {
	}

	public Telegram getTelegram() {
		return telegram;
	}

	public String getGatewayID() {
		return gatewayID;
	}
}
